import api from '../../api/api'
import * as types from '../mutation-types'

const state = {
  // 隐藏解析 答案  显示提交  点击选项按钮
  showAnswer: JSON.parse(localStorage.getItem('showAnswer')) || false,
  deepAnswer: JSON.parse(localStorage.getItem('deepAnswer')) || {},  //深度阅读 做题记录
  chooseAnswer: JSON.parse(localStorage.getItem('chooseAnswer')) || {},  //选词填空 做题记录
  matchAnswer: JSON.parse(localStorage.getItem('matchAnswer')) || {},  //阅读匹配 做题记录
  readAnswer: JSON.parse(localStorage.getItem('readAnswer')) || {},  //阅读简答 做题记录
  sectionAnswer: JSON.parse(localStorage.getItem('sectionAnswer')) || {},  //段落匹配 做题记录
  fillAnswer: JSON.parse(localStorage.getItem('fillAnswer')) || {},  // 阅读填空 做题记录
}

const actions = {
  // 解析全部(显示解析 答案 )
  showAnswer({ commit }, status) {
    commit(types.CON_SHOW_ANSWER, status)
  },

  // 深度阅读 做题记录
  deepAnswer({ commit }, res) {
    localStorage.setItem('deepAnswer', JSON.stringify(res))
    commit(types.CON_DEEP_ANSWER, res)
  },

  // 清除 深度阅读 做题记录
  clearAnswer({ commit }) {
    localStorage.removeItem('deepAnswer')
    commit(types.CON_DEEP_ANSWER, {})
  },

  // 选词填空 做题记录
  chooseAnswer({ commit }, res) {
    localStorage.setItem('chooseAnswer', JSON.stringify(res))
    commit(types.CON_CHOOSE_ANSWER, res)
  },

  // 清除 选词填空 做题记录
  clearChoose({ commit }) {
    localStorage.removeItem('chooseAnswer')
    commit(types.CON_CHOOSE_ANSWER, {})
  },

  // 阅读匹配 做题记录
  matchAnswer({ commit }, res) {
    localStorage.setItem('matchAnswer', JSON.stringify(res))
    commit(types.CON_MATCH_ANSWER, res)
  },

  // 清除 阅读匹配 做题记录
  clearMatch({ commit }) {
    localStorage.removeItem('matchAnswer')
    commit(types.CON_MATCH_ANSWER, {})
  },

  // 阅读简答 做题记录
  readAnswer({ commit }, res) {
    localStorage.setItem('readAnswer', JSON.stringify(res))
    commit(types.CON_SHORT_ANSWER, res)
  },

  // 清除 阅读简答 做题记录
  clearShort({ commit }) {
    localStorage.removeItem('readAnswer')
    commit(types.CON_SHORT_ANSWER, {})
  },

  // 段落匹配 做题记录
  sectionAnswer({ commit }, res) {
    localStorage.setItem('sectionAnswer', JSON.stringify(res))
    commit(types.CON_SECTION_ANSWER, res)
  },

  // 清除 段落匹配 做题记录
  clearSection({ commit }) {
    localStorage.removeItem('sectionAnswer')
    commit(types.CON_SECTION_ANSWER, {})
  },

  // 阅读填空 做题记录
  fillAnswer({ commit }, res) {
    localStorage.setItem('fillAnswer', JSON.stringify(res))
    commit(types.CON_FILL_ANSWER, res)
  },

  // 清除 阅读填空 做题记录
  clearFill({ commit }) {
    localStorage.removeItem('fillAnswer')
    commit(types.CON_FILL_ANSWER, {})
  },

}

const getters = {
  showAnswer: state => state.showAnswer,
  deepAnswer: state => state.deepAnswer,
  chooseAnswer: state => state.chooseAnswer,
  matchAnswer: state => state.matchAnswer,
  readAnswer: state => state.readAnswer,
  sectionAnswer: state => state.sectionAnswer,
  fillAnswer: state => state.fillAnswer,
}

const mutations = {
  [types.CON_SHOW_ANSWER](state, status) {
    state.showAnswer = status
    localStorage.setItem('showAnswer', status)
  },

  [types.CON_DEEP_ANSWER](state, res) {
    state.deepAnswer = res
  },

  [types.CON_CHOOSE_ANSWER](state, res) {
    state.chooseAnswer = res
  },

  [types.CON_MATCH_ANSWER](state, res) {
    state.matchAnswer = res
  },

  [types.CON_SHORT_ANSWER](state, res) {
    state.readAnswer = res
  },

  [types.CON_SECTION_ANSWER](state, res) {
    state.sectionAnswer = res
  },

  [types.CON_FILL_ANSWER](state, res) {
    state.fillAnswer = res
  },
}

export default {
  state,
  actions,
  getters,
  mutations
}
